package com.tf_cut.backend.payment.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.mapper.SellerConfigMapper;
import com.tf_cut.backend.payment.dao.SellerConfigRepository;
import com.tf_cut.backend.payment.pojo.ConfigSetting;
import com.tf_cut.backend.payment.service.SellerConfigService;
import com.tf_cut.backend.payment.vo.ConfigSettingVo;
import com.tf_cut.backend.util.ConfigSettingUtil;
import com.tf_cut.backend.util.Util;

@Service
@Transactional
public class SellerConfigServiceImpl implements SellerConfigService {
	
	@Autowired
	private SellerConfigMapper sellerConfigMapper;
	
	@Autowired
	private SellerConfigRepository sellerConfigRepository;
	
	@Resource
	private ConfigSettingUtil config;
	
	

	@Override
	public PageInfo<ConfigSettingVo> findListSeller(ConfigSettingVo configSettingVo) {
        com.github.pagehelper.PageHelper.startPage(configSettingVo.getPage(), configSettingVo.getRows());
        List<ConfigSettingVo> list = sellerConfigMapper.findListSeller(configSettingVo);
        com.github.pagehelper.PageInfo<ConfigSettingVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<ConfigSettingVo> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageInfo.getPageNum());
        returnPageInfo.setPageSize(pageInfo.getPageSize());
        returnPageInfo.setRows(pageInfo.getList());
        returnPageInfo.setRecords((int) pageInfo.getTotal());
        returnPageInfo.setTotal(pageInfo.getPages());
        return returnPageInfo;
	}



	@Override
	@Transactional
	public void configSave(ConfigSetting configSetting) {
		int type=0;
		if(configSetting.getConfigName() == null || configSetting.getConfigName().equals("")){
			configSetting.setCreateTime(Util.getDateNowDay());
			Integer sum=sellerConfigRepository.findByCount()+1;
			configSetting.setConfigName("invoice_service_type"+sum);
			type = 1;
		}else{
			ConfigSetting cos=sellerConfigRepository.findByName(configSetting.getConfigName());
			configSetting.setCreateTime(cos.getCreateTime());
			configSetting.setUpdateTime(Util.getDateNowDay());
		}
		configSetting.setConfigType("invoice_service_type");
		configSetting.setDescription("商户服务类型配置");
		if(type == 1){
			sellerConfigMapper.insertConfig(configSetting);
		}else{
			sellerConfigMapper.updateConfig(configSetting);
		}
		config.reloadConfigSetting("invoice_service_type");
	}



	@Override
	public ConfigSetting findByName(String id) {
		return sellerConfigRepository.findByName(id);
	}



	@Override
	@Transactional
	public void deleteByName(String id) {
		sellerConfigMapper.deleteConfig(id);
		config.reloadConfigSetting("invoice_service_type");
		
	}

}
